package com.english.tqc.db;

import java.util.ArrayList;
import java.util.HashMap;

import com.english.tqc.entity.User;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class DBHelper extends SQLiteOpenHelper {

	private StringBuffer  tableCreateBuffer;
	public static final String 		DB_NAME	 = "contact";
	public final static int VERSION	= 1;
	private static DBHelper instanceDbHelper = null;
	private SQLiteDatabase db;
	public static DBHelper getInstance(Context context){
		if(instanceDbHelper == null){
			instanceDbHelper = new DBHelper(context);
		}
		return instanceDbHelper;
	}
	
	private  void openDatabase(){
		if(db == null){
			db = this.getWritableDatabase();
		}
	}
	private  DBHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		tableCreateBuffer = new StringBuffer();
		tableCreateBuffer
		.append("create table user(_id integer primary key autoincrement,")
		.append("name text,")
		.append("mobilephone text,")
		.append("familyphone text,")
		.append("officephone text,")
		.append("address text,")
		.append("position text,")
		.append("company text,")
		.append("email text,")
		.append("othercontact text,")
		.append("zipcode text,")
		.append("remark text,")
		.append("imageid int );");
		arg0.execSQL(tableCreateBuffer.toString());
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String sqlString = "drop table if exists user";
		arg0.execSQL(sqlString);
		onCreate(arg0);
	}
	public long save(User user){
		openDatabase();
		ContentValues contentValues = new ContentValues();
		//contentValues.put("_id", user._id);
		contentValues.put("name", user.username);
		contentValues.put("mobilephone",user.mobilePhone);
		contentValues.put("familyphone", user.familyPhone);
		contentValues.put("officephone", user.officePhone);
		contentValues.put("address", user.address);
		contentValues.put("position", user.position);
		contentValues.put("company", user.company);
		contentValues.put("email", user.email);
		contentValues.put("othercontact", user.otherContact);
		contentValues.put("zipcode", user.zipCode);
		contentValues.put("remark", user.remark);
		contentValues.put("imageid", user.imageId);
		return db.insert("user", null, contentValues);
		
	}
	
	@SuppressWarnings("unchecked")
	public  ArrayList getUserList(){
		openDatabase();
		Cursor cursor = db.query("user", null, null, null, null, null, null);
		ArrayList list = new ArrayList();
		while(cursor.moveToNext()){
			HashMap map = new HashMap();
			map.put("_id",cursor.getString(cursor.getColumnIndex("_id")));
			map.put("officephone",cursor.getString(cursor.getColumnIndex("officephone")));
			map.put("familyphone",cursor.getString(cursor.getColumnIndex("familyphone")));
			map.put("address",cursor.getString(cursor.getColumnIndex("address")));
			map.put("position",cursor.getString(cursor.getColumnIndex("position")));
			map.put("company",cursor.getString(cursor.getColumnIndex("company")));
			map.put("email",cursor.getString(cursor.getColumnIndex("email")));
			map.put("othercontact",cursor.getString(cursor.getColumnIndex("othercontact")));
			map.put("zipcode",cursor.getString(cursor.getColumnIndex("zipcode")));
			map.put("remark",cursor.getString(cursor.getColumnIndex("remark")));
			
			map.put("_id",cursor.getInt(cursor.getColumnIndex("_id")));
			map.put("imageid",cursor.getInt(cursor.getColumnIndex("imageid")));
			map.put("name", cursor.getString(cursor.getColumnIndex("name")));
			map.put("mobilephone", cursor.getString(cursor.getColumnIndex("mobilephone")));
			list.add(map);
			
			}
		return list;
			
		
	}
} 
